<?php

namespace app\common\service;

use app\admin\service\SuperAdminService;
use app\common\dict\site\SiteAppTypeDict;
use app\common\exception\AuthException;
use app\site\service\site\SiteService;

class SiteCheckService extends BaseService
{



    public function checkSiteAuth(\think\Request $request)
    {

        // 获取当前请求的控制器名
        $controller = $request->url();


        $domain = $request->domain(true);

        //替换掉域名协议
        $domain = str_replace(['http://', 'https://'], '', $domain);


        //管理端
        $app_name = app('http')->getName();

        if(in_array($app_name,['admin'])) {
            $site_info = (new SuperAdminService())->getSiteInfo();
        }elseif (strpos($controller,'api/install')){
            $site_info = [
                'site_id' => 0,
                'app_type' => SiteAppTypeDict::SUPER_ADMIN,
                'domain' => '*',
                'name' => '安装系统',
                'status' => 1,
                'create_time' => time(),
                'update_time' => time(),
            ];
        }else{
            $site_info = (new siteService())->getSiteInfo(['domain' => $domain]);
        }

        //站点不存在
        if(empty($site_info)) throw new AuthException('站点不存在！'. $domain);

        if($site_info['status'] == 0) throw new AuthException('系统已到期或已禁用！');

        $request->siteId($site_info['site_id']);
        $request->appType($site_info['app_type']);
        return $site_info;
    }


}